package middleware

import (
	"dlxGoProject/global"
	email "dlxGoProject/pkg/Email"
	"dlxGoProject/pkg/app"
	"dlxGoProject/pkg/errcode"
	"fmt"
	"github.com/gin-gonic/gin"
	"time"
)

func Recovery() gin.HandlerFunc {
	defailMailer := email.NewEmail(&email.SMTPInfo{
		Host:     global.EmailSetting.Host,
		Port:     global.EmailSetting.Port,
		IsSSL:    global.EmailSetting.IsSSL,
		UserName: global.EmailSetting.UserName,
		Password: global.EmailSetting.Password,
		From:     global.EmailSetting.From,
	})
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				s:="panic recover err:%v"
				global.Logger.WithCallersFrames().Errorf(c,s,err)
				defailMailer.SendEmail(global.EmailSetting.To,
					fmt.Sprintf("抛出异常，发生时间：%d",time.Now().Unix()),
					fmt.Sprintf("错误信息：%v",err),)
				if err != nil {
					global.Logger.Panicf(c,"email.sendEmail err:%v",err)
				}
				app.NewResponse(c).ToErrorResponse(errcode.ServerError)
				c.Abort()
			}
		}()
		c.Next()
	}
}
